//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client.game.left;

import tv.dyndns.kishibe.client.Constant;
import tv.dyndns.kishibe.client.UserData;
import tv.dyndns.kishibe.client.packet.PacketMatchingPlayer;
import tv.dyndns.kishibe.client.packet.PacketProblemData;
import tv.dyndns.kishibe.client.util.SerialTimer;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.AbsolutePanel;

public class WidgetPlayerList extends AbsolutePanel {
	private static final int UPDATE_DURATION = 100;
	private WidgetPlayer players[];
	public static final int HEIGHT = 65;
	private final Timer timerUpdate = new SerialTimer() {
		public void run() {
			if (UserData.getInstance().isRankingMove()) {
				updatePosition();
			}
		}
	};

	public WidgetPlayerList(PacketMatchingPlayer[] players, int id) {
		setSize("180px", (65 * Constant.MAX_PLAYER_PER_SESSION) + "px");
		final int numberOfPlayers = players.length;
		this.players = new WidgetPlayer[numberOfPlayers];

		for (int i = 0; i < numberOfPlayers; ++i) {
			final PacketMatchingPlayer player = players[i];
			this.players[i] = new WidgetPlayer(player.name, player.imageFileName, this, 5, HEIGHT * i);
			add(this.players[i], this.players[i].getCurrentX(), this.players[i].getCurrentY());
		}

		updatePosition();
	}

	public void setPlayerName(String playerName[]) {
		for (int i = 0; i < playerName.length; ++i) {
			players[i].setPlayerName(playerName[i]);
		}
	}

	public WidgetPlayer getWidgetPlayer(int index) {
		if (index < 0 || players.length <= index) {
			return null;
		}

		return players[index];
	}

	public void openAnswer() {
		for (WidgetPlayer player : players) {
			player.open();
		}
	}

	public void showResult() {
		for (WidgetPlayer player : players) {
			player.recieveTimeUp();
		}
	}

	public void clearAnswer() {
		for (WidgetPlayer player : players) {
			player.clearAnswer();
		}
	}

	public void setProblem(PacketProblemData problem) {
		for (WidgetPlayer player : players) {
			player.setProblem(problem);
		}
	}

	private void updatePosition() {
		for (WidgetPlayer player : players) {
			player.updatePosition();
		}
	}

	protected void onLoad() {
		super.onLoad();
		timerUpdate.scheduleRepeating(UPDATE_DURATION);
	}

	protected void onUnload() {
		timerUpdate.cancel();
		super.onUnload();
	}
}
